package com.weibo.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.weibo.common.SessionManager;
import com.weibo.pojo.Tcomment;
import com.weibo.pojo.Tuser;
import com.weibo.pojo.Tweibo;
import com.weibo.service.IWeiboService;

/**
 * 主页
 * @author vorce
 *
 */
public class HomeAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -335693040130171127L;
	private IWeiboService weiboService;
	private List<Tweibo> weiboList;
	private Integer userId = 1;
	private String content;
	private Map<Integer,List<Tcomment>> commentMap;
	
	/**
	 * 提取要显示主页上的微博以及评论
	 * @return 
	 * @throws Exception
	 */
	public String show() throws Exception{
		
		Tuser user = (Tuser)SessionManager.getAttribute("nowUser");
		if(user != null){
			userId = user.getUserId();
		}
		weiboList = weiboService.getHomeWeiboList(userId);
		System.out.println(weiboList);
		commentMap = weiboService.getCommentMap(weiboList);
		return SUCCESS;
	}

	/**
	 * 发布微博
	 * @return
	 * @throws Exception
	 */
	public String publish() throws Exception{

		Tuser user = (Tuser)SessionManager.getAttribute("nowUser");
		String username = "";
		if(user != null){
			userId = user.getUserId();
			username = user.getUserName();
		}
		Tweibo weibo = new Tweibo();
		weibo.setWeiboCom(content);
		weibo.setWeiboOwner(userId);
		weibo.setWeiboOwnerName(username);
		Timestamp date = new Timestamp(new Date().getTime());
		weibo.setWeiboPubdate(date);
		System.out.println("it is published by" + weibo.getWeiboOwner());
		weiboService.saveWeibo(weibo);
		System.out.println("new weibo has been published.");
		return "home";
	}
	
	public IWeiboService getWeiboService() {
		return weiboService;
	}

	public void setWeiboService(IWeiboService weiboService) {
		this.weiboService = weiboService;
	}

	public List<Tweibo> getWeiboList() {
		return weiboList;
	}

	public void setWeiboList(List<Tweibo> weiboList) {
		this.weiboList = weiboList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Map<Integer, List<Tcomment>> getCommentMap() {
		return commentMap;
	}

	public void setCommentMap(Map<Integer, List<Tcomment>> commentMap) {
		this.commentMap = commentMap;
	}
	
}
